package com.andkrup.ds {
	import flash.events.IEventDispatcher;

	import com.andkrup.comm.unsubscribeLoadListener;	
	import com.andkrup.comm.subscribeLoadListener;	
	import com.andkrup.comm.ILoadListener;	
	
	import flash.net.URLLoader;	
	import flash.net.URLRequest;	
	import flash.events.EventDispatcher;	
	
	import com.adobe.net.URI;	
	import com.andkrup.ds.IResourceLoader;
	
	/**
	 * @author Andkrup
	 */
	public class BundleLoader extends EventDispatcher implements IResourceLoader {
		public function add(uri:URI, listener:ILoadListener):LoaderRequest{
			var loader:URLLoader = new URLLoader();
			// subscribe listener to all loader.contentLoader event dispatches
			subscribeLoadListener(loader, listener);
			
			var lr:LoaderRequest = new LoaderRequest(loader, new URLRequest(uri.toString()), uri);
			load(lr);
			return lr;
		}
		private function load(lr:LoaderRequest):void{
			lr.load();
		}
		
		public function remove(loaderRequest:LoaderRequest, listener:ILoadListener):void{
			// unsubscribe listener to all loader.contentLoader event dispatches
			unsubscribeLoadListener(loaderRequest.loader as IEventDispatcher, listener);
		}
	}
}
